home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / dir_util.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.7 KB  |  195 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.dir_util
  5.  
  6. Utility functions for manipulating directories and directory trees.'''
  7. __revision__ = '$Id: dir_util.py 60923 2008-02-21 18:18:37Z guido.van.rossum $'
  8. import os
  9. import sys
  10. from types import *
  11. from distutils.errors import DistutilsFileError, DistutilsInternalError
  12. from distutils import log
  13. _path_created = { }
  14.  
  15. def mkpath(name, mode = 511, verbose = 0, dry_run = 0):
  16.     """Create a directory and any missing ancestor directories.  If the
  17.        directory already exists (or if 'name' is the empty string, which
  18.        means the current directory, which of course exists), then do
  19.        nothing.  Raise DistutilsFileError if unable to create some
  20.        directory along the way (eg. some sub-path exists, but is a file
  21.        rather than a directory).  If 'verbose' is true, print a one-line
  22.        summary of each mkdir to stdout.  Return the list of directories
  23.        actually created."""
  24.     if not isinstance(name, StringTypes):
  25.         raise DistutilsInternalError, "mkpath: 'name' must be a string (got %r)" % (name,)
  26.     isinstance(name, StringTypes)
  27.     name = os.path.normpath(name)
  28.     created_dirs = []
  29.     if os.path.isdir(name) or name == '':
  30.         return created_dirs
  31.     if _path_created.get(os.path.abspath(name)):
  32.         return created_dirs
  33.     (head, tail) = os.path.split(name)
  34.     tails = [
  35.         tail]
  36.     while head and tail and not os.path.isdir(head):
  37.         (head, tail) = os.path.split(head)
  38.         tails.insert(0, tail)
  39.         continue
  40.         _path_created.get(os.path.abspath(name))
  41.     for d in tails:
  42.         head = os.path.join(head, d)
  43.         abs_head = os.path.abspath(head)
  44.         if _path_created.get(abs_head):
  45.             continue
  46.         
  47.         log.info('creating %s', head)
  48.         if not dry_run:
  49.             
  50.             try:
  51.                 os.mkdir(head)
  52.                 created_dirs.append(head)
  53.             except OSError:
  54.                 exc = None
  55.                 raise DistutilsFileError, "could not create '%s': %s" % (head, exc[-1])
  56.             except:
  57.                 None<EXCEPTION MATCH>OSError
  58.             
  59.  
  60.         None<EXCEPTION MATCH>OSError
  61.         _path_created[abs_head] = 1
  62.     
  63.     return created_dirs
  64.  
  65.  
  66. def create_tree(base_dir, files, mode = 511, verbose = 0, dry_run = 0):
  67.     """Create all the empty directories under 'base_dir' needed to
  68.        put 'files' there.  'base_dir' is just the a name of a directory
  69.        which doesn't necessarily exist yet; 'files' is a list of filenames
  70.        to be interpreted relative to 'base_dir'.  'base_dir' + the
  71.        directory portion of every file in 'files' will be created if it
  72.        doesn't already exist.  'mode', 'verbose' and 'dry_run' flags are as
  73.        for 'mkpath()'."""
  74.     need_dir = { }
  75.     for file in files:
  76.         need_dir[os.path.join(base_dir, os.path.dirname(file))] = 1
  77.     
  78.     need_dirs = need_dir.keys()
  79.     need_dirs.sort()
  80.     for dir in need_dirs:
  81.         mkpath(dir, mode, dry_run = dry_run)
  82.     
  83.  
  84.  
  85. def copy_tree(src, dst, preserve_mode = 1, preserve_times = 1, preserve_symlinks = 0, update = 0, verbose = 0, dry_run = 0):
  86.     """Copy an entire directory tree 'src' to a new location 'dst'.  Both
  87.        'src' and 'dst' must be directory names.  If 'src' is not a
  88.        directory, raise DistutilsFileError.  If 'dst' does not exist, it is
  89.        created with 'mkpath()'.  The end result of the copy is that every
  90.        file in 'src' is copied to 'dst', and directories under 'src' are
  91.        recursively copied to 'dst'.  Return the list of files that were
  92.        copied or might have been copied, using their output name.  The
  93.        return value is unaffected by 'update' or 'dry_run': it is simply
  94.        the list of all files under 'src', with the names changed to be
  95.        under 'dst'.
  96.  
  97.        'preserve_mode' and 'preserve_times' are the same as for
  98.        'copy_file'; note that they only apply to regular files, not to
  99.        directories.  If 'preserve_symlinks' is true, symlinks will be
  100.        copied as symlinks (on platforms that support them!); otherwise
  101.        (the default), the destination of the symlink will be copied.
  102.        'update' and 'verbose' are the same as for 'copy_file'."""
  103.     copy_file = copy_file
  104.     import distutils.file_util
  105.     if not dry_run and not os.path.isdir(src):
  106.         raise DistutilsFileError, "cannot copy tree '%s': not a directory" % src
  107.     not os.path.isdir(src)
  108.     
  109.     try:
  110.         names = os.listdir(src)
  111.     except os.error:
  112.         (errno, errstr) = None
  113.         if dry_run:
  114.             names = []
  115.         else:
  116.             raise DistutilsFileError, "error listing files in '%s': %s" % (src, errstr)
  117.         dry_run
  118.  
  119.     if not dry_run:
  120.         mkpath(dst)
  121.     
  122.     outputs = []
  123.     for n in names:
  124.         src_name = os.path.join(src, n)
  125.         dst_name = os.path.join(dst, n)
  126.         if preserve_symlinks and os.path.islink(src_name):
  127.             link_dest = os.readlink(src_name)
  128.             log.info('linking %s -> %s', dst_name, link_dest)
  129.             if not dry_run:
  130.                 os.symlink(link_dest, dst_name)
  131.             
  132.             outputs.append(dst_name)
  133.             continue
  134.         if os.path.isdir(src_name):
  135.             outputs.extend(copy_tree(src_name, dst_name, preserve_mode, preserve_times, preserve_symlinks, update, dry_run = dry_run))
  136.             continue
  137.         copy_file(src_name, dst_name, preserve_mode, preserve_times, update, dry_run = dry_run)
  138.         outputs.append(dst_name)
  139.     
  140.     return outputs
  141.  
  142.  
  143. def _build_cmdtuple(path, cmdtuples):
  144.     for f in os.listdir(path):
  145.         real_f = os.path.join(path, f)
  146.         if os.path.isdir(real_f) and not os.path.islink(real_f):
  147.             _build_cmdtuple(real_f, cmdtuples)
  148.             continue
  149.         cmdtuples.append((os.remove, real_f))
  150.     
  151.     cmdtuples.append((os.rmdir, path))
  152.  
  153.  
  154. def remove_tree(directory, verbose = 0, dry_run = 0):
  155.     """Recursively remove an entire directory tree.  Any errors are ignored
  156.     (apart from being reported to stdout if 'verbose' is true).
  157.     """
  158.     grok_environment_error = grok_environment_error
  159.     import distutils.util
  160.     log.info("removing '%s' (and everything under it)", directory)
  161.     if dry_run:
  162.         return None
  163.     cmdtuples = []
  164.     _build_cmdtuple(directory, cmdtuples)
  165.     for cmd in cmdtuples:
  166.         
  167.         try:
  168.             apply(cmd[0], (cmd[1],))
  169.             abspath = os.path.abspath(cmd[1])
  170.             if abspath in _path_created:
  171.                 del _path_created[abspath]
  172.         continue
  173.         except (IOError, OSError):
  174.             dry_run
  175.             exc = dry_run
  176.             log.warn(grok_environment_error(exc, 'error removing %s: ' % directory))
  177.             continue
  178.         
  179.  
  180.     
  181.  
  182.  
  183. def ensure_relative(path):
  184.     """Take the full path 'path', and make it a relative path so
  185.     it can be the second argument to os.path.join().
  186.     """
  187.     (drive, path) = os.path.splitdrive(path)
  188.     if sys.platform == 'mac':
  189.         return os.sep + path
  190.     if path[0:1] == os.sep:
  191.         path = drive + path[1:]
  192.     
  193.     return path
  194.  
  195.